<?php
/* 
  stats connection : 
- health increase per lvl = (100 + (lvl - 1) * 20) / 4 );  				          // loop from 1 to lvl for total hp
- lvl up xp = lvl * 50 + (lvl - 1) * (lvl * 10) 			      			      // starts from 5 wins and increases by 1
- total money won = ((lvl + 8) / 2 * (lvl - 1)) * 2			 		      		  // untill this lvl
- max stats (a/d) untill this lvl = 10 + total money won * 3/4					  // the max possible stats
- win xp = enemy_lvl * 10;				
- lose xp = enemy_lvl * 2;
- win money = 2$;
- reduce dmg =  def atm / max stats 											  // 10% - 75% reduce 
- avg turns per battle = 10
*/

class Battle{
	public static function createBattle(User $attacker, $defender){
		$dead = false;
		$first = $attacker;
		$second = $defender;	
		$turns = 0;
		while ($dead == false){
			$turns ++ ;
			$maxStats = (($defender->getLevel() + 9) / 2 * ($defender->getLevel())) * 3 / 2 ;
			$reduceCoef = $defender->getDefence() / $maxStats;
			if ($reduceCoef > 0.75){
				$reduceCoef = 0.75;														
			}
			if ($reduceCoef < 0.1){
				$reduceCoef = 0.1;
			}
			$dmg = $attacker->getAttack() - $attacker->getAttack() * $reduceCoef;
			$defender->setHealth($defender->getHealth() - $dmg);
			if ($defender->getHealth() > 0){
				$helper = $attacker;
				$attacker = $defender;
				$defender = $helper;
			}
			else {
				$dead = true;
			}
		}																					//change roles
		$winner = $attacker;
		$looser = $defender;
		$bot = new Profile("Компютъра");
		$currentDate = new DateTime();
		if ($first == $winner){
			echo 'Битката завърши ! Победи '.$looser->getUsername().' с '.round($winner->getHealth(), 0).' оставащ живот, за '.$turns.' хода! Поздравления !';
			if ($looser->getUsername() != "Компютъра"){ 
				//$msg1 = 'Битката завърши !  Загуби от'.$winner->getUsername().'. Той спечели с '.round($winner->getHealth(), 0).' оставащ живот, във '.$turns.' хода!';
				//$mail = new Mail($bot, $looser, $msg1 , $currentDate);
			}		
		}
		else{
			echo 'Битката завърши !  Загуби от '.$winner->getUsername().'. Той спечели с '.round($winner->getHealth(), 0).' оставащ живот, във '.$turns.' хода!';
			if ($winner->getUsername() != "Компютъра"){
				//$msg1 =  'Битката завърши ! Победи '.$looser->getUsername().' с '.round($winner->getHealth(), 0).' оставащ живот, за '.$turns.' хода! Поздравления !';
				//$mail = new Mail($bot, $winner, $msg1, $currentDate);
			}
		}			
		$winner->setExperience($winner->getExperience() + ($looser->getLevel() * 10));
		$looser->setExperience($looser->getExperience() + ($winner->getLevel() * 2));
		
		$winner->setMoney($winner->getMoney() + 2);
		if ($second->getUsername() == "Компютъра"){
			$first->setEnergy($first->getEnergy() - 1);
		}
		$looser->setLockedPeriod(5);
		$looser->setLockedTime($currentDate);
		$winner->setWins();
		$looser->setLooses();
		if ($winner->getUsername() != "Компютъра"){
			$lvlUpXp = $winner->getLevel() * 50 + ($winner->getLevel() - 1) * ( $winner->getLevel() * 10);			//lvl up
			if ($winner->getExperience() >= $lvlUpXp){
				//$mail = new Mail($bot, $winner, "Ти качи ниво (".$winner->getLevel()."). Честито !", $currentDate);
				$winner->setLevel($winner->getLevel() + 1);
				$winner->setExperience($winner->getExperience() - $lvlUpXp);
			}
		}
		if ($looser->getUsername() != "Компютъра"){
			$lvlUpXp = $looser->getLevel() * 50 + ($looser->getLevel() - 1) * ( $looser->getLevel() * 10);
			if ($looser->getExperience() >= $lvlUpXp){											
				//$mail = new Mails($bot,  $looser, "Ти качи ниво (".$winner->getLevel()."). Честито !", $currentDate);
				$looser->setLevel($looser->getLevel() + 1);
				$looser->setExperience($looser->getExperience() - $lvlUpXp);
			}
		}
		$reset = new Profile($looser->getUsername());
		$helpHealth = 0;																						//health reset
		for ( $i = 1 ; $i <= $reset->getUser()->getLevel() ; $i ++){
			$helpHealth = ($helpHealth + (100 + ($i - 1) * 20) / 4 );
		}
		$reset->getUser()->setHealth($helpHealth);
		$reset = new Profile($winner->getUsername());
		$helpHealth = 0;																						
		for ( $i = 1 ; $i <= $reset->getUser()->getLevel() ; $i ++){
			$helpHealth = ($helpHealth + (100 + ($i - 1) * 20) / 4 );
		
		}
		$reset->getUser()->setHealth($helpHealth);
	}

	public static function computerBattle(User $attacker){
		if (get_class($attacker) != 'User'){
			throw new Exception("The attacker is not an object of User !");
		}
		if  ($attacker->getEnergy() <= 0){
			throw new Exception("The attacker does not have enough energy !");
		}
		
		$attacker->setCompBattles();
		$bot = new Profile('bot');
		$defender = $bot->getUser();
		
		$helpLevel = rand($attacker->getLevel() - 1, $attacker->getLevel() + 1);
		if ($helpLevel <= 0){
			$helpLevel = 1;
		}
		$defender->setLevel($helpLevel);
		
		$helpHealth = 0;
		for ( $i = 1 ; $i <= $helpLevel ; $i ++){
			$helpHealth = ($helpHealth + (100 + ($i - 1) * 20) / 4 );
		}
		$defender->setHealth($helpHealth);	
															
		$helpStats = (($defender->getLevel() + 8) / 2 * ($defender->getLevel() - 1)) * 3 / 2;
		if ($defender->getLevel() == 1){
			$helpStats = 10;
		}				
		$defender->setAttack($helpStats * rand(2,6) / 10);												
		$defender->setDefence($helpStats - $defender->getAttack());
		$defender->setExperience(0);
		$defender->setMoney(0);
		$defender->setEnergy(1);
		self::createBattle($attacker, $defender);
	}
}
?>